package vip.xiaonuo.modular.manage.bigridcovert.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.star.bridge.oleautomation.Decimal;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.xml.xsi.XSISimpleTypes;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.gaode.CoordinateConvertAddr;
import vip.xiaonuo.modular.manage.bigridcovert.entity.BiGridCovert;
import vip.xiaonuo.modular.manage.bigridcovert.mapper.BiGridCovertMapper;
import vip.xiaonuo.modular.manage.bigridcovert.service.BiGridCovertService;
import vip.xiaonuo.util.GenerateUniqueIdUtil;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.Iterator;

@Service
public class BiGridCovertServiceImpl extends ServiceImpl<BiGridCovertMapper, BiGridCovert> implements BiGridCovertService {


    @Override
    public void uploadData() throws IOException {
        File shapeFile = new File("D:\\ALL\\内网通1\\文件管理\\CY-HP\\2022\\2022格网化指标\\格网化指标_武汉\\格网指标_武汉.shp");
        ShapefileDataStore store = new ShapefileDataStore(shapeFile.toURI().toURL());

        Charset charset = Charset.forName("UTF-8");
        store.setCharset(charset);
        SimpleFeatureSource sfSource = store.getFeatureSource();
        SimpleFeatureIterator featureIterator = sfSource.getFeatures().features();

        int i = 0;
        int j = 0;
        while (featureIterator.hasNext()) {
            i++;
            SimpleFeature feature = featureIterator.next();
            Iterator<? extends Property> iterator = feature.getValue().iterator();

            while (iterator.hasNext()) {
                j++;
                Property property = iterator.next();
                if (j==1) {
                    String[] split = property.getValue().toString()
                            .replace("(", "")
                            .replace(")", "")
                            .split(",");
                    String location = split[split.length - 1].trim().replace(" ", ",");
                    String[] locationAddr = CoordinateConvertAddr.getLocationAddr(location);
//
                    BiGridCovert biGridCovert=new BiGridCovert();

                    biGridCovert.setBigGridId(GenerateUniqueIdUtil.getGuid());

                    biGridCovert.setLng(new BigDecimal(locationAddr[0]));
                    biGridCovert.setLat(new BigDecimal(locationAddr[1]));
                    biGridCovert.setProvName(locationAddr[2]);
                    biGridCovert.setCityName(locationAddr[3]);
                    biGridCovert.setCountryName(locationAddr[4]);
                    this.save(biGridCovert);
                }
            }
            j = 0;
        }
    }
}
